package cc.sunni.service.impl;

import cc.sunni.dao.UserMapper;
import cc.sunni.entity.User;
import cc.sunni.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
 * @author admin
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 构建用户表查询条件
     * 1、条件默认eq，需要模糊查询请手动修改
     * 2、默认会过滤掉为空的查询条件
     */
    private LambdaQueryWrapper<User> buildQuery(User user) {
        LambdaQueryWrapper<User> condition = Wrappers.lambdaQuery(User.class);
        condition.orderByDesc(User::getCreateTime, User::getUpdateTime);
        if (user == null) {
            return condition;
        }
        if (user.getId() != null) {
            condition.eq(User::getId, user.getId());
        }
        if (!StringUtils.isEmpty(user.getUsername())) {
            condition.eq(User::getUsername, user.getUsername());
        }
        if (user.getCreateTime() != null) {
            condition.eq(User::getCreateTime, user.getCreateTime());
        }
        if (!StringUtils.isEmpty(user.getCreateUser())) {
            condition.eq(User::getCreateUser, user.getCreateUser());
        }
        if (user.getUpdateTime() != null) {
            condition.eq(User::getUpdateTime, user.getUpdateTime());
        }
        if (!StringUtils.isEmpty(user.getUpdateUser())) {
            condition.eq(User::getUpdateUser, user.getUpdateUser());
        }
        return condition;
    }
}